<?php 
function msubstr($str, $start, $len) {
	$tmpstr = "";
	$strlen = $start + $len;
	for($i = 0; $i < $strlen; $i++) {
		if(ord(substr($str, $i, 1)) > 0xa0) {
			$tmpstr .= substr($str, $i, 2);
			$i++;
		} 
		else
			$tmpstr .= substr($str, $i, 1);
		}
		return $tmpstr;
	}
	function cut_str($sourcestr,$cutlength)
	{
		$returnstr='';
		$i=0;
		$n=0;
		$str_length=strlen($sourcestr);//字符串的字节数
		while (($n<$cutlength) and ($i<=$str_length))
		{
			$temp_str=substr($sourcestr,$i,1);
			$ascnum=Ord($temp_str);//得到字符串中第$i位字符的ascii码
			if ($ascnum>=224) //如果ASCII位高与224，
			{
				$returnstr=$returnstr.substr($sourcestr,$i,3); //根据UTF-8编码规范，将3个连续的字符计为单个字符
				$i=$i+3; //实际Byte计为3
				$n++; //字串长度计1
			}
			elseif ($ascnum>=192) //如果ASCII位高与192，
			{
				$returnstr=$returnstr.substr($sourcestr,$i,2); //根据UTF-8编码规范，将2个连续的字符计为单个字符
				$i=$i+2; //实际Byte计为2
				$n++; //字串长度计1
			}
			elseif ($ascnum>=65 && $ascnum<=90) //如果是大写字母，
			{
				$returnstr=$returnstr.substr($sourcestr,$i,1);
				$i=$i+1; //实际的Byte数仍计1个
				$n++; //但考虑整体美观，大写字母计成一个高位字符
			}
			else //其他情况下，包括小写字母和半角标点符号，
			{
				$returnstr=$returnstr.substr($sourcestr,$i,1);
				$i=$i+1; //实际的Byte数计1个
				$n=$n+0.5; //小写字母和半角标点等与半个高位字符宽...
			}
		}
		if ($str_length>$cutlength){
			$returnstr = $returnstr . "...";//超过长度时在尾处加上省略号
		}
		return $returnstr;
	}
?>

<div class="content">
	<div class="title">
	<h2>学习论坛</h2>
	</div>
	<div class="forumindexblock">
		<div class="topsearch">
			<div class="searchbox">
				<form action="<?php echo $this->baseUrl().'/forum/search';?>" method="post">
				<input type="text" value="搜索" id="searchforum" name="searchforum" 
					onblur="if(this.value=='') this.value='搜索'; this.style.color='#999'; return true;"
					onfocus="if(this.value=='搜索') this.value='';this.style.color='#333'; return true;"/>
					<button type="submit">
					</button>
					</form>
			</div>
			<a href="<?php echo $this->baseUrl().'/forum/newforum'?>"><div class="newforum">发表新帖</div></a>
		</div>
		<ul>
			<?php
				foreach($this->allforums as $forum)
				{
				   
				    
			        echo '<li><div class="count" title="';
			        if($forum['replytoid']==0)
			        {
			            echo $forum['countreply'];
			            echo '">'.$forum['countreply'].'</div>';
			        }
			        else
			        {
			            echo '本帖是回复贴';
			            echo '">'.'回复贴'.'</div>';
			        }
				    echo '<div class="content"><div class="ti"><a href="';
				    if($forum['replytoid']==0)echo $this->baseUrl().'/forum/detail?forumid='.$forum['id'].'">';
				    else echo $this->baseUrl().'/forum/detail?forumid='.$forum['replytoid'].'">';
				    echo $this->escape($forum['title']).'</a></div>'.'<div class="content">';
				    $shortcontent = cut_str($forum['content'],25);
				    
				    //$shortcontent = $forum['content'];
				    echo $this->escape($shortcontent).'</div></div><div class="author"><p>by  ';
				    echo $this->escape($forum['author']).'</p><p>';
				    date_default_timezone_set("Asia/Shanghai");
				    echo gmdate('Y-m-d',$forum['time']).'</p></div></li>';
				} 
			?>
			<!-- 静态模板  
			<li>
			<div class="count" title="26个回复">26</div>
			<div class="content">
				<div class="ti"><a href="<?php echo $this->baseUrl().'/forum/detail?forumid='?>">哎呀！这离散数学还让不让人活 了！！</a></div>
				<div class="content">今天冯老师的考试让我们。。。。</div>
			</div>
			<div class="author">
			<p>by 志坚</p>
			<p>2012.7.25</p>
			</div>
			</li> -->
		</ul>
		<div class="page">
		<a href="<?php echo $this->baseUrl().'/forum/index?page='.'1';?>">首页</a>
		<a href="<?php echo $this->baseUrl().'/forum/index?page='.($this->page-1);?>">上一页</a>

		<?php
			//echo Zend_Registry::get('config');
			if($this->pagecount<=8)
			{
			    $begin = 1;
			    $end = $this->pagecount;//显示8页
			}
			else 
			{
			    if($this->pagecount-$this->page+1<=8)
			    {
			        $begin = $this->pagecount-7;
			        $end = $this->pagecount;
			    }
			    else 
			    {
			        $begin = $this->page;
			        $end = $this->page + 7;
			    }
			    
			}
			for($i = $begin;$i<=$end;$i++)
			{
			    if($i==$this->page)echo $i;//本页页数没有超链接
			 	else 
			 	{
			 	    echo '<a href="';
			 	    echo $this->baseUrl().'/forum/index?page='.$i.'"';
			 	    echo '>'.$i.'</a>';
			 	}
			} 
		?>
		<a href="<?php echo $this->baseUrl().'/forum/index?page='.($this->page+1);?>"> 下一页 </a>  
		<a href="<?php echo $this->baseUrl().'/fourm/index?page='.$this->pagecount;?>">尾页</a>
		</div>
	</div>
	<div class="forumsideblock">
		<img  alt="img" src="<?php echo $this->baseUrl().'/'.$this->authorimg;?>"/>
		<div class="tip">
			<h4><?php echo $this->escape($this->author);?></h4>
			<?php 
				if($this->author!='游客')
				{
				    echo '<p><a href="'.$this->baseUrl().'/forum/changeimg'.'">修改头像</a></p>';
				    echo '<p><a href="'.$this->baseUrl().'/forum/myforum'.'">我发表的帖子</a></p>';
				    echo '<p><a href="'.$this->baseUrl().'/forum/myreplyforum'.'">我回复的帖子</a></p>';
				}
			?>
		</div>
	</div>

</div>
